//
//  SDPhotoCollectionCell.m
//  SKinDiary
//
//  Created by pcyang on 16/7/15.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDPhotoCollectionCell.h"
#import "SDPhotoInfo.h"
#import "SDSimpleImageView.h"

@interface SDPhotoCollectionCell()
@property(nonatomic,strong)SDSimpleImageView* photoView;
@property(nonatomic,strong)UIImageView* selectedIconView;
@property(nonatomic,assign)BOOL editable;
@property(nonatomic,strong)UIImageView* cloudImageView;
@end

@implementation SDPhotoCollectionCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
     //   UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onPotoViewClicked:)];
      //  [self addGestureRecognizer:tapGestureRecognizer];
        
        _photoView = [[SDSimpleImageView alloc] initWithFrame:self.bounds];
        _photoView.layer.masksToBounds = true;
        _photoView.contentMode = UIViewContentModeScaleAspectFill;
        _photoView.defaultImage = [UIImage imageNamed:@"gallery_photo_empty.png"];
        [self.contentView addSubview:_photoView];
        
        UIImage* cloudImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"gallery_photo_icon_cloud_only.png");
        _cloudImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cloudImg.size.width, cloudImg.size.height)];
        _cloudImageView.image = cloudImg;
        [self.contentView addSubview:_cloudImageView];
        _cloudImageView.hidden = YES;
        _cloudImageView.right = self.width - 2;
        _cloudImageView.bottom = self.height - 2;
        
        _overlayView = [[UIView alloc] initWithFrame:_photoView.bounds];
        _overlayView.backgroundColor = [UIColor colorWithRGB:0xEEF1F5];
        _overlayView.alpha = 0.2f;
        _overlayView.hidden = YES;
        [self.contentView addSubview:_overlayView];
        
        UIImage* selectedImg = [UIImage imageNamed:@"gallery_photo_icon_selected.png"];
        _selectedIconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, selectedImg.size.width, selectedImg.size.height)];
        _selectedIconView.image = selectedImg;
        [self.contentView addSubview:_selectedIconView];
        _selectedIconView.hidden = YES;
        _selectedIconView.left = (self.width - _selectedIconView.width)/2;
        _selectedIconView.top = (self.height - _selectedIconView.height)/2;
    }

    return self;
}

-(void)setEditable:(BOOL)editable
{
    _editable = editable;
    
    if (!editable) {
        [self setOverlayViewHidden:YES];
    }
}
-(void)setOverlayViewHidden:(BOOL)hidden
{
    _overlayView.hidden = hidden;
    _selectedIconView.hidden = hidden;
}

-(void)setData:(SDPhotoInfo*)item
{
    _photoInfo = item;
    
   // _cloudImageView.hidden = YES;
    if (CHECK_VALID_STRING(_photoInfo.originalFileName)) {
        NSString* directory = [SDFile getDirectoryInCacheWithName:kPhotoDiretoryName];
        NSString *filePath = [directory stringByAppendingPathComponent:_photoInfo.originalFileName];
        if ([SDFile fileExist:filePath]) {
            _photoView.image = [UIImage imageWithContentsOfFile:filePath];
            _photoView.url = @"";
            
            if (CHECK_VALID_STRING(item.photoUrl)) {
                UIImage* cloudImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"gallery_photo_icon_synced.png");
                _cloudImageView.image = cloudImg;
                
                _cloudImageView.hidden = NO;
            } else {
                
            }
        }
    } else {
        UIImage* cloudImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"gallery_photo_icon_synced.png");
        _cloudImageView.image = cloudImg;
        if (CHECK_VALID_STRING(item.photoUrl)) {
            NSString* cropUrl = [item.photoUrl stringByAppendingString:@"@100h_100w_1e_1c"];
            [_photoView setImageWithURL:cropUrl];
        }
    }
    
    if (_editable) {
        [self setOverlayViewHidden:!_photoInfo.selected];
    } else {
        _photoInfo.selected = NO;
        [self setOverlayViewHidden:YES];
    }
    
    _cloudImageView.hidden = !CHECK_VALID_STRING(_photoInfo.photoUrl);
}

-(void)onPotoViewClicked:(UITapGestureRecognizer*)gestureRecognizer
{
    if (_delegate && [_delegate respondsToSelector:@selector(photoViewClicked:)]) {
        
        if (_editable) {
            [self setOverlayViewHidden:!_overlayView.hidden];
            _photoInfo.selected = !_overlayView.hidden;
            [_delegate photoViewClicked:_photoInfo];
        }
    }
}
@end
